//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Web.UI.WebControls;
namespace DotNetNuke.UI.UserControls
{
	public abstract class DualListControl : Framework.UserControlBase
	{
		private string _ListBoxWidth = "";
		private string _ListBoxHeight = "";
		private ArrayList _Available;
		private ArrayList _Assigned;
		private string _DataTextField = "";
		private string _DataValueField = "";
		private bool _Enabled = true;
		protected System.Web.UI.WebControls.ListBox lstAvailable;
		protected System.Web.UI.WebControls.LinkButton cmdAdd;
		protected System.Web.UI.WebControls.LinkButton cmdRemove;
		protected System.Web.UI.WebControls.LinkButton cmdAddAll;
		protected System.Web.UI.WebControls.LinkButton cmdRemoveAll;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.ListBox lstAssigned;
		private string MyFileName = "DualListControl.ascx";
		public string ListBoxWidth
		{
			get { return Convert.ToString(ViewState[this.ClientID + "_ListBoxWidth"]); }
			set { _ListBoxWidth = value; }
		}
		public string ListBoxHeight
		{
			get { return Convert.ToString(ViewState[this.ClientID + "_ListBoxHeight"]); }
			set { _ListBoxHeight = value; }
		}
		public ArrayList Available
		{
			get
			{
				ArrayList objList = new ArrayList();
				foreach (ListItem objListItem in lstAvailable.Items)
				{
					objList.Add(objListItem);
				}
				return objList;
			}
			set { _Available = value; }
		}
		public ArrayList Assigned
		{
			get
			{
				ArrayList objList = new ArrayList();
				foreach (ListItem objListItem in lstAssigned.Items)
				{
					objList.Add(objListItem);
				}
				return objList;
			}
			set { _Assigned = value; }
		}
		public string DataTextField
		{
			set { _DataTextField = value; }
		}
		public string DataValueField
		{
			set { _DataValueField = value; }
		}
		public bool Enabled
		{
			set { _Enabled = value; }
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdAdd.Click += new EventHandler(cmdAdd_Click);
			cmdAddAll.Click += new EventHandler(cmdAddAll_Click);
			cmdRemove.Click += new EventHandler(cmdRemove_Click);
			cmdRemoveAll.Click += new EventHandler(cmdRemoveAll_Click);

			try
			{
				Label1.Text = Services.Localization.Localization.GetString("Available", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				Label2.Text = Services.Localization.Localization.GetString("Assigned", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				cmdAdd.ToolTip = Services.Localization.Localization.GetString("Add", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				cmdAddAll.ToolTip = Services.Localization.Localization.GetString("AddAll", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				cmdRemove.ToolTip = Services.Localization.Localization.GetString("Remove", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				cmdRemoveAll.ToolTip = Services.Localization.Localization.GetString("RemoveAll", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				if (!Page.IsPostBack)
				{
					if (!String.IsNullOrEmpty(_ListBoxWidth))
					{
						lstAvailable.Width = System.Web.UI.WebControls.Unit.Parse(_ListBoxWidth);
						lstAssigned.Width = System.Web.UI.WebControls.Unit.Parse(_ListBoxWidth);
					}
					if (!String.IsNullOrEmpty(_ListBoxHeight))
					{
						lstAvailable.Height = System.Web.UI.WebControls.Unit.Parse(_ListBoxHeight);
						lstAssigned.Height = System.Web.UI.WebControls.Unit.Parse(_ListBoxHeight);
					}
					lstAvailable.DataTextField = _DataTextField;
					lstAvailable.DataValueField = _DataValueField;
					lstAvailable.DataSource = _Available;
					lstAvailable.DataBind();
					Sort(lstAvailable);
					lstAssigned.DataTextField = _DataTextField;
					lstAssigned.DataValueField = _DataValueField;
					lstAssigned.DataSource = _Assigned;
					lstAssigned.DataBind();
					Sort(lstAssigned);
					lstAvailable.Enabled = _Enabled;
					lstAssigned.Enabled = _Enabled;
					ViewState[this.ClientID + "_ListBoxWidth"] = _ListBoxWidth;
					ViewState[this.ClientID + "_ListBoxHeight"] = _ListBoxHeight;
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdAdd_Click(object sender, System.EventArgs e)
		{
			ArrayList objList = new ArrayList();
			foreach (ListItem objListItem in lstAvailable.Items)
			{
				objList.Add(objListItem);
			}
			foreach (ListItem objListItem in objList)
			{
				if (objListItem.Selected)
				{
					lstAvailable.Items.Remove(objListItem);
					lstAssigned.Items.Add(objListItem);
				}
			}
			lstAvailable.ClearSelection();
			lstAssigned.ClearSelection();
			Sort(lstAssigned);
		}
		private void cmdRemove_Click(object sender, System.EventArgs e)
		{
			ArrayList objList = new ArrayList();
			foreach (ListItem objListItem in lstAssigned.Items)
			{
				objList.Add(objListItem);
			}
			foreach (ListItem objListItem in objList)
			{
				if (objListItem.Selected)
				{
					lstAssigned.Items.Remove(objListItem);
					lstAvailable.Items.Add(objListItem);
				}
			}
			lstAvailable.ClearSelection();
			lstAssigned.ClearSelection();
			Sort(lstAvailable);
		}
		private void cmdAddAll_Click(object sender, System.EventArgs e)
		{
			foreach (ListItem objListItem in lstAvailable.Items)
			{
				lstAssigned.Items.Add(objListItem);
			}
			lstAvailable.Items.Clear();
			lstAvailable.ClearSelection();
			lstAssigned.ClearSelection();
			Sort(lstAssigned);
		}
		private void cmdRemoveAll_Click(object sender, System.EventArgs e)
		{
			foreach (ListItem objListItem in lstAssigned.Items)
			{
				lstAvailable.Items.Add(objListItem);
			}
			lstAssigned.Items.Clear();
			lstAvailable.ClearSelection();
			lstAssigned.ClearSelection();
			Sort(lstAvailable);
		}
		private void Sort(ListBox ctlListBox)
		{
			ArrayList arrListItems = new ArrayList();
			foreach (ListItem objListItem in ctlListBox.Items)
			{
				arrListItems.Add(objListItem);
			}
			arrListItems.Sort(new ListItemComparer());
			ctlListBox.Items.Clear();
			foreach (ListItem objListItem in arrListItems)
			{
				ctlListBox.Items.Add(objListItem);
			}
		}
	}
	public class ListItemComparer : IComparer
	{
		public int Compare(object x, object y)
		{
			ListItem a = (ListItem)x;
			ListItem b = (ListItem)y;
			CaseInsensitiveComparer c = new CaseInsensitiveComparer();
			return c.Compare(a.Text, b.Text);
		}
	}
}
